/*
 ***************************************************************************
 * MediaTeK Inc.
 * 4F, No. 2 Technology	5th	Rd.
 * Science-based Industrial	Park
 * Hsin-chu, Taiwan, R.O.C.
 *
 * (c) Copyright 2002-2012, MTK.
 *
 * All rights reserved.	MediaTeK's source	code is	an unpublished work	and	the
 * use of a	copyright notice does not imply	otherwise. This	source code
 * contains	confidential trade secret material of MediaTeK Tech. Any attemp
 * or participation	in deciphering,	decoding, reverse engineering or in	any
 * way altering	the	source code	is stricitly prohibited, unless	the	prior
 * written consent of MediaTeK, Inc. is obtained.
 ***************************************************************************

	Module Name:
	enhanced_security_ctl_me_func.c

	Abstract:

	Revision History:
	Who			When			What
	--------	----------		----------------------------------------------
	Name		Date			Modification logs
	lisa.xue	2012/7/28	Create
*/
#include <stdio.h>
#include <string.h>
#include "omci_adpt_common.h"
#include "omci_me_enhanced_security_ctl.h"


int setEnhancedSecurityCtlOLTCryptoCapabilities(omci_me_info_ptr me_info, omci_attr_dscp_ptr omci_attr);
int setEnhancedSecurityCtlOLTChallengeStatus(omci_me_info_ptr me_info, omci_attr_dscp_ptr omci_attr);
int setEnhancedSecurityCtlOLTResultStatus(omci_me_info_ptr me_info, omci_attr_dscp_ptr omci_attr);
int setEnhancedSecurityCtlBroadcastKeyTB(omci_me_info_ptr me_info, omci_attr_dscp_ptr omci_attr);

/*******************************************************************************************************************************
						9.13.11: Enhanced security control

********************************************************************************************************************************/
static attribute_func_t enhancedSecurityCtlBroadcastKeyTBFunc = {
    NULL,
	setEnhancedSecurityCtlBroadcastKeyTB
};

omci_attr_dscp_t omci_attr_enhanced_security_ctrl_op[] = {
{0, "meId",							2,  ATTR_ACCESS_R,      ATTR_FORMAT_UNSIGNED_INT,    ATTR_FULLY_SUPPORT,&generalAttrFunc},
{1, "OLT crypto capabilities",		16, ATTR_ACCESS_W,      ATTR_FORMAT_STRING,      	 ATTR_FULLY_SUPPORT,&generalAttrFunc},
{2, "OLT random challenge tb",		17, ATTR_ACCESS_R_W,    ATTR_FORMAT_TABLE,           ATTR_FULLY_SUPPORT,&generalAttrFunc},
{3, "OLT challenge status",			1,  ATTR_ACCESS_R_W,    ATTR_FORMAT_ENUM,            ATTR_FULLY_SUPPORT,&generalAttrFunc},
{4, "ONU selected crypto",			1,  ATTR_ACCESS_R,      ATTR_FORMAT_UNSIGNED_INT,    ATTR_FULLY_SUPPORT,&generalAttrFunc},
{5, "ONU random challenge tb",		16, ATTR_ACCESS_R,      ATTR_FORMAT_TABLE,           ATTR_FULLY_SUPPORT,&generalAttrFunc},
{6, "ONU authentication result tb",	16, ATTR_ACCESS_R,      ATTR_FORMAT_TABLE,           ATTR_FULLY_SUPPORT,&generalAttrFunc},
{7, "OLT authentication result tb",	17, ATTR_ACCESS_W,      ATTR_FORMAT_TABLE,           ATTR_FULLY_SUPPORT,&generalAttrFunc},
{8, "OLT result status",			1,  ATTR_ACCESS_R_W,    ATTR_FORMAT_ENUM,            ATTR_FULLY_SUPPORT,&generalAttrFunc},
{9, "ONU authentication state",		1,  ATTR_ACCESS_R,      ATTR_FORMAT_ENUM,            ATTR_FULLY_SUPPORT,&generalAttrFunc},
{10, "master session key name",		16, ATTR_ACCESS_R,      ATTR_FORMAT_STRING,          ATTR_FULLY_SUPPORT,&generalAttrFunc},
{11, "broadcast key table",			18, ATTR_ACCESS_R_W,    ATTR_FORMAT_TABLE,           ATTR_FULLY_SUPPORT,&enhancedSecurityCtlBroadcastKeyTBFunc},
{12, "effective key length",		2,  ATTR_ACCESS_R,      ATTR_FORMAT_UNSIGNED_INT,    ATTR_FULLY_SUPPORT,&generalAttrFunc},
{255, "", 							0, 	0, 0, 0, 	NULL}
};

omci_me_func_t omci_me_enhanced_security_ctrl_op ={
	omci_me_general_create_op,
	omci_me_general_delete_op,
	omci_me_general_set_op,
	omci_me_general_get_op,
};

omci_enhanced_security_ctrl_t omci_enhanced_security_ctrl_attr_data = {0};

omci_attr_t omci_enhanced_security_ctrl_attr={
	(uint8_t *)&omci_enhanced_security_ctrl_attr_data,
	sizeof(omci_enhanced_security_ctrl_attr_data),
};

/*******************************************************************************************************************************
						9.13.11: Enhanced security control

********************************************************************************************************************************/

int setEnhancedSecurityCtlBroadcastKeyTB(omci_me_info_ptr me_info, omci_attr_dscp_ptr omci_attr)
{
    int ret = 0;
	broadcastKeyCtrl_t keyControl;
	omci_enhanced_security_ctrl_ptr enhanced_sec_ctrl_attr = NULL;
	
	omcidbgPrintf(OMCI_DEBUG_LEVEL_TRACE, "Enter [%s][%d]\n",__FUNCTION__,__LINE__);
    if(NULL == me_info){
        omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, " [%s][%d] parameter is NULL, fail\n",__FUNCTION__,__LINE__);
        ret = -1;
        goto end;
    }
	enhanced_sec_ctrl_attr = (omci_enhanced_security_ctrl_ptr)me_info->attributes;
    if(enhanced_sec_ctrl_attr == NULL ){
        omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, " [%s][%d] attributes is NULL, fail\n",__FUNCTION__,__LINE__);
        ret = -1;
        goto end;
    }
    if(enhanced_sec_ctrl_attr->broadcastKeyTable.buf == NULL ){
        omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, " [%s][%d] table buf is NULL, fail\n",__FUNCTION__,__LINE__);
        ret = -1;
        goto end;
    }
	keyControl.raw = *(enhanced_sec_ctrl_attr->broadcastKeyTable.buf);
	
    if(0 == keyControl.bit.opr)
    {
        ret = xgponmgr_lib_set_omci_broadcast_key((uint8_t *)enhanced_sec_ctrl_attr->broadcastKeyTable.buf);
        if(0 != ret){
            omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]ERROR: return error.\n", __FUNCTION__, __LINE__);
        }
    }else if((1 == keyControl.bit.opr)||(2 == keyControl.bit.opr))
    {
        ret = xgponmgr_lib_clean_omci_broadcast_key((uint8_t *)enhanced_sec_ctrl_attr->broadcastKeyTable.buf);
        if(0 != ret){
            omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]ERROR: return error.\n", __FUNCTION__, __LINE__);
        }
    }else
    {
        omcidbgPrintf(OMCI_DEBUG_LEVEL_ERROR, "[%s][%d]ERROR: parameter error.\n", __FUNCTION__, __LINE__);
        return -1;
    }

	ret = 0;
end:	
    return ret;
}


